﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.Model.Enum;
using System.Data;
using System.Text;
using HZ.Supervise.IBLL;

namespace HZ.Supervise.Web.Secondphase.Print
{
    public partial class PrintPaymentVoucher : BasePage
    {
        public PrintPaymentVoucher()
        {
            this.ModuleNo = Module.企业统计分析.ToString();
            this.PageOperateType = OperateType.支付单.ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(Request.QueryString["CompanyID_PS"]))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('页面跳转时未能将配送企业编号传递到本页面，请在页面返回后重试！');window.close();</script>");
            }
        }

        protected void btnPreview_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtTimeFrom.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('请选择开始时间！');</script>");
            }
            if (String.IsNullOrEmpty(txtTimeTo.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('请选择结束时间！');</script>");
            }
            DateTime? timeFrom = new HZ.Common.ConvertHelper(txtTimeFrom.Text).ToDateTime;
            DateTime? timeTo = new HZ.Common.ConvertHelper(txtTimeTo.Text).ToDateTime;

            if (timeFrom >= timeTo)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('结束时间小于或等于开始时间，请重新设置时间！');</script>");
            }

            string strwhere = "";

            switch (ddlHosType.SelectedValue)
            {
                case "0":
                    break;
                case "1":
                    strwhere = " and Levels in('3','4','5','6')";
                    break;
                case "2":
                    strwhere = " and IsCountryDetail in('1','2','3','4')";
                    break;
                case "3":
                    strwhere = " and IsCountryDetail ='2'";
                    break;
                case "4":
                    strwhere = " and IsCountryDetail ='1'";
                    break;
                default:
                    break;
            }

            if (!string.IsNullOrEmpty(ddlIsGov.SelectedValue))
            {
                strwhere += " and IsGovernment=" + ddlIsGov.SelectedValue;
            }

            IBLL_Statistics ibs = HZ.Supervise.BLLFactory.IBLL_Statistics();
            DataTable dtdetail = ibs.GetCompanyAmountStatistics(Request.QueryString["CompanyID_PS"], timeFrom, timeTo,strwhere);

            if (dtdetail.Rows.Count > 0)
            {
                AmountConvertToChinese actc = new AmountConvertToChinese();
                
                StringBuilder str = new StringBuilder();
                decimal warehouseamount = 0;
                decimal returnamount = 0;
                decimal payamount = 0;
                for (int i = 0; i < dtdetail.Rows.Count; i++)
                {
                    str.Append("<tr><td>");
                    str.Append((i + 1) + "");
                    str.Append("</td><td>");
                    str.Append(dtdetail.Rows[i]["CompanyName"].ToString());
                    str.Append("</td><td>");
                    str.Append(dtdetail.Rows[i]["WarehouseMonth"].ToString());
                    str.Append("</td><td>");
                    str.Append(dtdetail.Rows[i]["WarehouseAmount"].ToString());
                    str.Append("</td><td>");
                    str.Append(dtdetail.Rows[i]["ReturnAmount"].ToString());
                    str.Append("</td><td>");
                    str.Append(dtdetail.Rows[i]["PayAmount"].ToString());
                    str.Append("</td></tr>");
                    warehouseamount += Convert.ToDecimal(dtdetail.Rows[i]["WarehouseAmount"].ToString());
                    returnamount += Convert.ToDecimal(dtdetail.Rows[i]["ReturnAmount"].ToString());
                    payamount += Convert.ToDecimal(dtdetail.Rows[i]["PayAmount"].ToString());
                }
                str.Append("<tr><td>");
                str.Append((dtdetail.Rows.Count + 1) + "");
                str.Append("</td><td>");
                str.Append("合计：");
                str.Append("</td><td>");
                str.Append("");
                str.Append("</td><td>");
                str.Append(warehouseamount.ToString());
                str.Append("</td><td>");
                str.Append(returnamount.ToString());
                str.Append("</td><td>");
                str.Append(payamount.ToString());
                str.Append("</td></tr>");
                litContent.Text = str.ToString();
                btnPrintDetail.Visible = true;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('所选地区在所选时间段内无数据，请重新填写条件！');</script>");
                btnPrintDetail.Visible = false;
            }
        }
    }
}
